//
//  NICustomTabBar.m
//  AIWAYS
//
//  Created by Marx on 2018/9/25.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NICustomTabBar.h"
@interface NICustomTabBar()
{
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIButton *firstButton;
    __weak IBOutlet UIButton *thirdButton;
    __weak IBOutlet UIButton *secondButton;
    
    NSInteger selectIndex;
    
}

@end
@implementation NICustomTabBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    [self awakeFromNib];
    selectIndex = 0;
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
}


- (IBAction)onClickTabButton:(id)sender {
    
    NSInteger index = ((UIButton *)sender).tag;
    [self setSelectIndex:index];
}

- (void)setSelectIndex:(NSInteger)index{
    if (index != selectIndex) {
        selectIndex = index;
        [self.delegate selectTabButton:index];
        
        
    }
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    CGPoint hitPoint = [self convertPoint:point toView:secondButton];
    if ([secondButton pointInside:hitPoint withEvent:event]) {
        return secondButton;
    }else{
        return [super hitTest:point withEvent:event];
    }
}

@end
